package com.yanqu.road.logic.union;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.union.UnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarPatronsData;
import com.yanqu.road.entity.union.UnionWarProcedure;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class UnionWarManger {

    static List<UnionWarProcedure> procedureList = new ArrayList<>();

    public static void parseUnionWarDate() {
        String[] paramArr = GameConfig.UNION_WAR_DATE.split("V");
        for (String procedureStr : paramArr) {
            String[] procedures = procedureStr.split(";");
            UnionWarProcedure unionWarProcedure = new UnionWarProcedure(procedures);
            procedureList.add(unionWarProcedure);
        }
    }

    /**
     * 获取场次标识
     * @param time
     * @return
     */
    private static int getMark(long time) {
        for (int i = 0; i < procedureList.size(); i++) {
            if (procedureList.get(i).isThisProcedure(time)) {
                return procedureList.get(i).getStartTimeMark(time);
            }
        }
        return 0;
    }

    public static int getNowMark() {
        return getMark(System.currentTimeMillis());
    }

    /**
     * 是否是备战期
     */
    public static boolean isPrepareTime() {
        long nowTime = System.currentTimeMillis();
        for (int i = 0; i < procedureList.size(); i++) {
            if (procedureList.get(i).getPrepareTime().isInTime(nowTime)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是鼓舞期
     */
    public static boolean isDonateTime() {
        long nowTime = System.currentTimeMillis();
        for (int i = 0; i < procedureList.size(); i++) {
            if (procedureList.get(i).getDonateTime().isInTime(nowTime)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是领奖期
     */
    public static boolean isRewardTime() {
        long nowTime = System.currentTimeMillis();
        for (int i = 0; i < procedureList.size(); i++) {
            if (procedureList.get(i).getRewardTime().isInTime(nowTime)) {
                return true;
            }
        }
        return false;
    }

    static class UnionWarPatronsSort implements Comparator<UnionWarPatronsData> {

        @Override
        public int compare(UnionWarPatronsData o1, UnionWarPatronsData o2) {
            int result = 0;
//            int result = o2.getPosition() - o1.getPosition();
//            if (result != 0) {
//                return -result;
//            }
//            long time = o1.getTime() - o2.getTime();
//            result = time > 0 ? 1 : (time == 0 ? 0 : -1);
            return result;
        }
    }

    /**
     * 给人才排序
     * @param unionWarDetail
     */
    public static void orderUserPatronsList(UnionWarDetail unionWarDetail) {
        unionWarDetail.setPatronsList(new ArrayList<>());
        for (long userId : unionWarDetail.getUserUnionWarInfoMap().keySet()) {
//            for (UnionWarPatronsData patronsData : unionWarDetail.getUserDataMap().get(userId).getPatronsMap().values()) {
//                patronsData.setPosition(unionWarDetail.getUserDataMap().get(userId).getPosition());
//                patronsData.setBookType(unionWarDetail.getUserDataMap().get(userId).getBookId());
//                patronsData.setNickName(unionWarDetail.getUserDataMap().get(userId).getNickName());
//                patronsData.setUserId(unionWarDetail.getUserDataMap().get(userId).getUserId());
//                unionWarDetail.getPatronsList().add(patronsData);
//            }
        }
        unionWarDetail.getPatronsList().sort(new UnionWarPatronsSort());
    }

    /**
     * 获取真实参战人数
     * @param unionWarDetail
     * @return
     */
    public static int getUnionWarJoinUserCount(UnionWarDetail unionWarDetail) {
        if (null == unionWarDetail) {
            return 0;
        }
        int count = 0;
        for (long userId : unionWarDetail.getUserUnionWarInfoMap().keySet()) {
//            if (unionWarDetail.getUserDataMap().get(userId).getPatronsMap().size() > 0) {
//                count++;
//            }
        }
        return count;
    }

}
